package com.xhwy.controller;

import com.xhwy.entity.Address;
import com.xhwy.param.AddressParam;
import com.xhwy.param.AddressRemoveParam;
import com.xhwy.service.AddressService;
import com.xhwy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//地址的controller
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @PostMapping("getAllByUserId")
    public R getAllByUserId(@RequestBody @Validated AddressParam addressParam, BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，查询失败");
        }
        return addressService.getAllByUserId(addressParam);
    }

    @PostMapping("add")
    public R add(@RequestBody @Validated Address address, BindingResult result){
        System.out.println(address);
        if (result.hasErrors()){
            return R.fail("参数异常，添加失败");
        }
        return addressService.add(address);
    }

    @DeleteMapping("delete")
    public R delete(@RequestBody @Validated AddressRemoveParam addressRemoveParam, BindingResult result){
        if (result.hasErrors()){
            return R.fail("参数异常，删除失败");
        }
        return addressService.delete(addressRemoveParam);
    }
}
